<template>
  <div class="login">
    <el-form label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="name"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="password" type="password"></el-input>
      </el-form-item>
      <div style="text-align: center">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      name: "",
      password: "",
    };
  },
  methods: {
    cancel() {
      this.$bus.$emit("closed", 1);
    },
    confirm() {
      if (this.name == "") {
        this.$message.error("请输入用户名");
      } else if (this.password == "") {
        this.$message.error("请输入密码");
      } else {
        this.$http
          .postRequest("main/user/login", {
            name: this.name,
            password: this.password,
          })
          .then((res) => {
            this.$message.success(res.msg);
            if (res.code == 0) {
              sessionStorage.setItem("bfid", res.data.id);
              sessionStorage.setItem("bfname", res.data.name);
              this.$bus.$emit("closed", 1);
            }
          })
          .catch((error) => {
            console.log(error);
          });
      }
    },
  },
};
</script>
<style scoped>
</style>
